<?php
namespace Company\Controller;
use Think\Controller;
class RelevantController extends BaseController {
	
	public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        C('erp_dbname',$g['db']);
    }
    //部门列表
    public function section(){
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Section->where($map)->count();
		$list=$Section->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	public function cost(){
		$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Cost->where($map)->count();
		$list=$Cost->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	public function bill(){
		$Bill=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Bill->where($map)->count();
		$list=$Bill->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//人员列表
    public function personnel(){
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$Personnel->where($map)->count();
		$list=$Personnel->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'))->where('no="'.$v['section_no'].'"')->find();
			$list[$k]['section_name']=$section['name'];
		}
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//单位列表
    public function company(){
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
        
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['no'] = array('like','%'.$keywords.'%');
			}
			if($selval=='2'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$ContactsCompany->where($map)->count();
		$list=$ContactsCompany->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

        $this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
        $this->display();            
    }
	//添加、更新部门信息
	function add_section(){
		if(IS_POST){
			$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_section($param,0);//验证输入字符，参数2为0时表示更新
				$res=$Section->save($param);
				// echo $Section->getlastsql();
				// die;
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_section($param,1);//验证输入字符,参数2为1时表示添加
				// $data['no']=1;
				$res=$Section->add($param);

				if($res){
					$this->success('添加成功');
				}
			}
			
		}else{
			$this->display();
		}
	}
	//添加、更新
	function add_bill(){
		if(IS_POST){
			$Bill=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_bill($param,0);//验证输入字符，参数2为0时表示更新
				$res=$Bill->save($param);
				// echo $Bill->getlastsql();
				// die;
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_bill($param,1);//验证输入字符,参数2为1时表示添加
				// $data['no']=1;
				$res=$Bill->add($param);

				if($res){
					$this->success('添加成功');
				}
			}
			
		}else{
			$this->display();
		}
	}
	//添加、更新
	function add_cost(){
		if(IS_POST){
			$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_bill($param,0);//验证输入字符，参数2为0时表示更新
				$res=$Cost->save($param);
				// echo $Cost->getlastsql();
				// die;
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_bill($param,1);//验证输入字符,参数2为1时表示添加
				// $data['no']=1;
				$res=$Cost->add($param);

				if($res){
					$this->success('添加成功');
				}
			}
			
		}else{
			$this->display();
		}
	}
	//添加、更新单位信息
	function add_company(){
		if(IS_POST){
			$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_company($param,0);//验证输入字符，参数2为0时表示更新
				$res=$ContactsCompany->save($param);
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_company($param,1);//验证输入字符,参数2为1时表示添加
				$res=$ContactsCompany->add($param);
				// echo $ContactsCompany->getlastsql();
				// die;
				if($res){
					$this->success('添加成功');
				}
			}
			
		}else{
			$this->display();
		}
	}
	//添加、更新员工信息
	function add_personnel(){
		if(IS_POST){
			$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$id=$param['id'];
			if($id){	//更新
				$param['update_time']=time();
				$this->validate_personnel($param,0);//验证输入字符，参数2为0时表示更新
				$res=$Personnel->save($param);
				if($res!==false){
					$this->success('更新成功');
				}
			}else{	//添加
				$param['add_time']=time();
				$this->validate_personnel($param,1);//验证输入字符,参数2为1时表示添加
				$res=$Personnel->add($param);
				if($res){
					$this->success('添加成功');
				}
			}
			
		}else{
			$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$section_list=$Section->select();
			$this->assign('section_list',$section_list);
			$this->display();
		}
	}
	//编辑单位信息
	function edit_company(){
		$id=trim(I('id'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			
		$detail=$ContactsCompany->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//编辑员工信息
	function edit_personnel(){
		$id=trim(I('id'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$section_list=$Section->select();
		$this->assign('section_list',$section_list);
			
		$detail=$Personnel->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//编辑部门信息
	function edit(){
		$id=trim(I('id'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$Section->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//编辑发票类型
	function edit_bill(){
		$id=trim(I('id'));
		$Bill=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$Bill->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//编辑
	function edit_cost(){
		$id=trim(I('id'));
		$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$detail=$Cost->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除公司信息
	function del_company(){
		$id=trim(I('id'));
		$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$ContactsCompany->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//删除员工信息
	function del_personnel(){
		$id=trim(I('id'));
		$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$Personnel->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//删除部门
	function del(){
		$id=trim(I('id'));
		$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$Section->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//删除发票类型
	function del_bill(){
		$id=trim(I('id'));
		$Bill=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		
		$status=$Bill->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//验证输入字符
	function validate_section($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$Section=M('Section', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('部门编码不能为空！');
			}
			if($param['name']==''){
				$this->error('部门名称不能为空！');
			}
		
			if($status){ //添加重复判断
				$section_no=$Section->where(array('no'=>$param['no']))->find();
				$section_name=$Section->where(array('name'=>$param['name']))->find();
			}else{  //更新重复判断
				$section_no=$Section->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$section_name=$Section->where(array('name'=>$param['name'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($section_no){
				$this->error('部门编号重复！');
			}
			if($section_name){
				$this->error('部门名称重复！');
			}
	}
	//验证输入字符
	function validate_bill($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$Bill=M('BillCate', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('编码不能为空！');
			}
			if($param['name']==''){
				$this->error('名称不能为空！');
			}
		
			if($status){ //添加重复判断
				$bill_no=$Bill->where(array('no'=>$param['no']))->find();
				$bill_name=$Bill->where(array('name'=>$param['name']))->find();
			}else{  //更新重复判断
				$bill_no=$Bill->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$bill_name=$Bill->where(array('name'=>$param['name'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($bill_no){
				$this->error('发票类型编号重复！');
			}
			if($bill_name){
				$this->error('发票类型名称重复！');
			}
	}
	//验证输入字符
	function validate_cost($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$Cost=M('Cost', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('编码不能为空！');
			}
			if($param['name']==''){
				$this->error('名称不能为空！');
			}
		
			if($status){ //添加重复判断
				$bill_no=$Cost->where(array('no'=>$param['no']))->find();
				$bill_name=$Cost->where(array('name'=>$param['name']))->find();
			}else{  //更新重复判断
				$bill_no=$Cost->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$bill_name=$Cost->where(array('name'=>$param['name'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($bill_no){
				$this->error('编号重复！');
			}
			if($bill_name){
				$this->error('名称重复！');
			}
	}
	//验证输入字符
	function validate_personnel($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$Personnel=M('Personnel', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('员工编码不能为空！');
			}
			if($param['name']==''){
				$this->error('员工姓名不能为空！');
			}
			if($param['section_no']==''){
				$this->error('所在部门不能为空！');
			}
		
			if($status){ //添加重复判断
				$personnel_no=$Personnel->where(array('no'=>$param['no']))->find();
				$personnel_name=$Personnel->where(array('name'=>$param['name']))->find();
			}else{  //更新重复判断
				$personnel_no=$Personnel->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$personnel_name=$Personnel->where(array('name'=>$param['name'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($section_no){
				$this->error('员工编码重复！');
			}
			if($section_name){
				$this->error('员工姓名重复！');
			}
	}
	//验证输入字符
	function validate_company($param,$status=1){ //status为1表示添加操作，为0表示更新操作
			$ContactsCompany=M('ContactsCompany', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			if($param['no']==''){
				$this->error('员工编码不能为空！');
			}
			if($param['name']==''){
				$this->error('员工姓名不能为空！');
			}
			if($status){ //添加重复判断
				$company_no=$ContactsCompany->where(array('no'=>$param['no']))->find();
				$company_name=$ContactsCompany->where(array('name'=>$param['name']))->find();
			}else{  //更新重复判断
				$company_no=$ContactsCompany->where(array('no'=>$param['no'],'id'=>array('neq',$param['id'])))->find();
				$company_name=$ContactsCompany->where(array('name'=>$param['name'],'id'=>array('neq',$param['id'])))->find();
			}
			
			if($section_no){
				$this->error('公司编码重复！');
			}
			if($section_name){
				$this->error('公司名称重复！');
			}
	}
    //and so on...
}